﻿<%@ WebHandler Language="C#" Class="wxMiniProgramLogin" %>
using Models;
using System;
using System.Web;
using Newtonsoft.Json;
using System.Linq;
using System.Web.Configuration;   
using Newtonsoft.Json.Linq;


public class wxMiniProgramLogin : IHttpHandler {
         public static string APPID =WebConfigurationManager.AppSettings["APPID"];
        public static string APPSECRET =WebConfigurationManager.AppSettings["APPSECRET"];
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";


        //encryptedData: "tCw="
        //iv: "scOln9/Qqzc/lxoGfmaunw=="
        //session_key: "VACD4DwcRvTYaw5PaBcbQQ=="
        //signature: "27256f918f8ba0ec14631985e4ec9409cc787b05"

        string postdata=Utils.PostInput(context);

        JObject jo = (JObject)JsonConvert.DeserializeObject(postdata);
        string session_key = jo["session_key"].ToString();

        if (string.IsNullOrEmpty(session_key))
        {
            HttpContext.Current.Response.Write("parameter error");
            return;
        }
        var encryptedData= jo["encryptedData"].ToString();
        var iv= jo["iv"].ToString();
        var sessionKey= jo["session_key"].ToString();

        ///public static string Decrypt(string encryptedData, string iv, string sessionKey)
        var wxresult= wxHelper.Decrypt(encryptedData,iv,sessionKey);
        var mytoken = (JObject)JsonConvert.DeserializeObject(wxresult);




        //avatarUrl: "https132"
        //city: "Kunming"
        //country: "China"
        //gender: 1
        //language: "zh_CN"
        //nickName: "李锋"
        //openId: "oONIh0XW7FIxBOBPKBzqYJg5iUAw"
        //province: "Yunnan"
        //watermark: {timestamp: 1607327204, appid: "wx66a6e096f3fef998"}
        //appid: "wx66a6e096f3fef998"
        //timestamp: 1607327204


        var openid = mytoken["openId"].ToString();
        using (Entities db = new Entities())
        {
            var wxuser = db.User.FirstOrDefault(p => p.UOpenID == openid);
            if (wxuser == null)//如果没有微信信息，表示第一次进入系统，
            {
                wxuser = new User();
                wxuser.UGuid = _rky.GenerateStringID();
                wxuser.UOpenID = openid;
                wxuser.UNickName = mytoken["nickName"].ToString();
                wxuser.UHeadImg = mytoken["avatarUrl"].ToString();
                wxuser.Ustate = 0;
                wxuser.Ujifen = 0;
                wxuser.RegTime = DateTime.Now;

                //  wxuser.UIndirectParentId = pid;  //直接推荐人

                db.User.Add(wxuser);
                db.SaveChanges();
            }
        };

        var data = new
        {
            data =new
            {
                mytoken,
             token=openid 
            },
            code = 1,
            msg = "登陆",
            time = DateTime.Now
        };
        Formatting microsoftDataFormatSettings = default(Formatting);
        string result = JsonConvert.SerializeObject(data, microsoftDataFormatSettings);
        context.Response.Write(result);

        HttpContext.Current.Response.End();
        //context.Response.Write("Hello World");


    }





    public bool IsReusable {
        get {
            return false;
        }
    }

}